define([ 'angular' ], function(angular) {
	'use strict';
	var myservices = angular.module('myservices',[]);
	//检查用户本地session保存的服务
	myservices.service('sessionService', ['$window',function($window) {
		//从本地获取保存的x-session－token
		this.gologin=false;
		if($window.localStorage){
			this.xsessiontoken=localStorage.getItem("x-session-token");
			if(this.xsessiontoken==null||this.xsessiontoken==undefined){
				this.gologin=true;
			}
		};
	}]);

	myservices.service('GoodsTypeService',['$q','$http',function($q,$http){
		return {
			query:function(){
				var deferred=$q.defer();
				$http({
					method:'POST',
					url:'../mall/goods/goodstype/findAll.action'
				}).
				success(function(data,status,headers,config){
					deferred.resolve(data);
				}).
				error(function(data,status,headers,config){
					deferred.reject(data);
				});
				return deferred.promise;
			}
		};
	}]);

	myservices.service('brandService',['$q','$http',function($q,$http){
		return{
			query:function(){
				var deferred=$q.defer();
				$http({
					method:'POST',
					url:'../mall/brand/findAll.action'
				}).
				success(function(data,status,headers,config){
					deferred.resolve(data);
				}).
				error(function(data,status,headers,config){
					deferred.reject(data);
				});
				return deferred.promise;
			}
		};
	}]);

	myservices.service('goodscategoryService',['$q','$http',function($q,$http){
		return{
			query:function(){
				var deferred=$q.defer();
				$http({
					method:'POST',
					url:'../mall/goods/goodscategory/findNavAll.action'
				}).
				success(function(data,status,headers,config){
					deferred.resolve(data);
				}).
				error(function(data,status,headers,config){
					deferred.reject(data);
				});
				return deferred.promise;
			}
		};
	}]);
	
	myservices.service('productskuService',['$q','$http',function($q,$http){
		return{
			query:function(){
				var deferred=$q.defer();
				$http({
					method:'POST',
					url:'../mall/productsku/findAll.action'
				}).
				success(function(data,status,headers,config){
					deferred.resolve(data);
				}).
				error(function(data,status,headers,config){
					deferred.reject(data);
				});
				return deferred.promise;
			}
		};
	}]);

	myservices.factory('Common', [
	                    		'$http', '$q', function($http, $q) {
	                    			return {
	                    				loadScript: function(url, callback) {
	                    					var head = document.getElementsByTagName("head")[0];
	                    					var script = document.createElement("script");
	                    					script.setAttribute("type", "text/javascript");
	                    					script.setAttribute("src", url);
	                    					script.setAttribute("async", true);
	                    					script.setAttribute("defer", true);
	                    					head.appendChild(script);
	                    					//fuck ie! duck type
	                    					if (document.all) {
	                    						script.onreadystatechange = function() {
	                    							var state = this.readyState;
	                    							if (state === 'loaded' || state === 'complete') {
	                    								callback && callback();
	                    							}
	                    						}
	                    					}
	                    					else {
	                    						//firefox, chrome
	                    						script.onload = function() {
	                    							callback && callback();
	                    						}
	                    					}
	                    				},
	                    				loadCss: function(url) {
	                    					var ele = document.createElement('link');
	                    					ele.href = url;
	                    					ele.rel = 'stylesheet';
	                    					if (ele.onload == null) {
	                    						ele.onload = function() {
	                    						};
	                    					}
	                    					else {
	                    						ele.onreadystatechange = function() {
	                    						};
	                    					}
	                    					angular.element(document.querySelector('body')).prepend(ele);
	                    				}
	                    			}
	                    		}
	                    	]);
	
	return myservices;

});
